package com.cs543.j.client.ui;


import com.cs543.j.client.R;
import com.cs543.j.client.cloudService.filesystem.Entry;
import com.cs543.j.client.cloudService.filesystem.File;
import com.cs543.j.client.cloudService.storage.CloudStorage;
import com.cs543.j.client.cloudService.storage.Storage;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TextView;

public class RenameActionDialog extends android.support.v4.app.DialogFragment {

	Entry selected;
	Storage storage;

	public RenameActionDialog(Entry selected, Storage storage) {
		super();
		this.selected = selected;
		this.storage = storage;
	}
	
	TextView fileNameTextView;
	Dialog resultDialog;
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		builder.setView(inflater.inflate( R.layout.storage_explorer_rename, null))
			.setPositiveButton("Rename", new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					fileNameTextView = (TextView) resultDialog.findViewById(R.id.storage_explorer_rename_text);					
					((CloudStorage)storage).move(selected, new File(fileNameTextView.getText().toString(), selected.getParent()));
				}
			})
			.setNegativeButton("Cancel", null);
		;
		
		resultDialog = builder.create();		
		return resultDialog;
	}
	@Override
	public void onStart() {
		super.onStart();
		
		fileNameTextView = (TextView) resultDialog.findViewById(R.id.storage_explorer_rename_text);
		fileNameTextView.setText(selected.getName());
	}
	
	
	
}
